//
//  RequestManager.m
//  PKTProject
//
//  Created by young on 15/5/30.
//  Copyright (c) 2015年 young. All rights reserved.
//

#import "RequestManager.h"
#import "AFURLRequestSerialization.h"
#define serverUrl @""

@implementation RequestManager


+ (void)POST:(NSString *)URL params:(NSDictionary * )params success:(void (^)(id response))success
     failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error
{
    // 创建请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    // 请求超时时间
    manager.requestSerializer.timeoutInterval = 30;
    NSString *postStr = URL;
    NSMutableDictionary *dict = [params mutableCopy];

    // 发送post请求
    [manager POST:postStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        // 请求成功
        NSDictionary *responseDict = (NSDictionary *)responseObject;
        success(responseDict);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {// 请求失败
        Error( operation,error);
        
    }];
    
    
}

+ (void)GET:(NSString *)URL
    success:(void (^)(id response))success
    failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error
{
    // 获得请求管理者
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setHTTPShouldHandleCookies:NO];
    manager.requestSerializer.timeoutInterval = 30;
    NSString *getStr = URL;

    // 发送GET请求
    [manager GET:getStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *responseDict = (NSDictionary *)responseObject;
        success(responseDict);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (!operation.responseObject) {
            NSLog(@"网络错误");
        }
        Error( operation,error);
    }];
    
    
}


+ (AFHTTPRequestOperationManager *)manager
{
    static AFHTTPRequestOperationManager *manager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPRequestOperationManager manager];
        // 发送的为json
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        
        //接受为json
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        
        manager.requestSerializer.timeoutInterval = 30;
       
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
  
        manager.operationQueue.maxConcurrentOperationCount = 1;
    });
    
    return manager;
}


+ (void)UPLOADIMAGE:(NSString *)URL
             params:(NSDictionary *)params
        uploadImage:(UIImage *)image
            success:(void (^)(id response))success
            failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error
{
    // 创建请求管理者
    AFHTTPRequestOperationManager *manager = [self manager];

    NSMutableDictionary *dict = [params mutableCopy];
    
    [manager POST:URL parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSData *imageData = UIImageJPEGRepresentation(image, 1);
     
        
        
        [formData appendPartWithFileData:imageData name:@"bookingFile[file_data]" fileName:@"head.jpg" mimeType:@"image/jpeg"];

        
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *responseDict = (NSDictionary *)responseObject;
        success(responseDict);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        Error( operation,error);
        
    }];
}




@end
